package com.zt.teacher.controller;

import com.zt.common.common.ApiException;
import com.zt.common.pojo.Course;
import com.zt.common.pojo.common.JsonResult;
import com.zt.common.pojo.common.SemesterStatus;
import com.zt.common.utils.ClassUtil;
import com.zt.teacher.dto.StartClassDTO;
import com.zt.teacher.service.CourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * description:
 * author:zt
 * date:2021-12-16
 */
@RestController
@CrossOrigin
@RequestMapping("course")
@Api("课程模块")
public class CourseController {
    
    @Autowired
    private CourseService courseService;


    @GetMapping("getAll")
    @ApiOperation("获取所有课程")
    public JsonResult<List<Course>> getCourse(){
        List<Course> courses = courseService.getAllCourse();
        return JsonResult.success(courses);
    }

    @PostMapping("insert")
    @ApiOperation("新建课程")
    @RequiresRoles("admin")
    public JsonResult<Integer> addCourse(@RequestBody Course course){
        int courseId = courseService.insert(course);
        return JsonResult.success(courseId);
    }

    @DeleteMapping("delete/{courseId}")
    @ApiOperation("删除指定课程")
    @RequiresRoles("admin")
    public JsonResult delCourse(@PathVariable int courseId){
        courseService.delCourse(courseId);
        return JsonResult.success();
    }

    @PostMapping("update")
    @ApiOperation("修改课程")
    @RequiresRoles("admin")
    public JsonResult updateCourse(@RequestBody Course course){
        courseService.updateCourse(course);
        return JsonResult.success();
    }

    @GetMapping("getSemester/{n}")
    @ApiOperation("获取未来n个学期及其状态")
    public JsonResult<ArrayList<SemesterStatus>> getSemester(@PathVariable int n){

        return JsonResult.success(ClassUtil.getSemesterStatus(n));
    }

    @PostMapping("startClass")
    @ApiOperation("设置开课")
    @RequiresRoles("admin")
    public JsonResult setStartClass(@RequestBody StartClassDTO startClassDTO){
        try {
            courseService.setStartClass(startClassDTO.getSemester(),startClassDTO.getExpired());
        }catch (ApiException e){
            return JsonResult.error("-1",e.getMes());
        }
        return JsonResult.success();
    }

    @PostMapping("selectClass")
    @ApiOperation("设置选课")
    @RequiresRoles("admin")
    public JsonResult setSelectClass(@RequestBody StartClassDTO startClassDTO){
        try {
            courseService.setSelectClass(startClassDTO.getSemester(),startClassDTO.getExpired());
        }catch (ApiException e){
            return JsonResult.error("-1",e.getMes());
        }
        return JsonResult.success();
    }
}
